package com.dkm.ec.cart.domain;

import java.math.BigDecimal;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.ec.cart.persistence.dao.CartEntityDao;
import com.dkm.ec.cart.persistence.entity.CartEntityPo;
import com.dkm.ec.cart.persistence.entity.CartItemPo;
import com.dkm.ec.cart.repository.CartEntityRepository;
import com.dkm.ec.cart.repository.CartItemRepository;
import com.dkm.ec.product.persistence.entity.ProductMediaPo;
import com.dkm.ec.product.repository.ProductMediaRepository;
import com.dkm.ec.sale.persistence.entity.SaleSkuPo;

/**
 * 对象功能:购物车 领域对象实体
 * 开发公司:dkm.com
 * 开发人员:ZHH
 * 创建时间:2017-05-17 23:30:36
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class CartEntity extends AbstractDomain<String, CartEntityPo>{
	 
	 private CartEntityDao cartEntityDao = null;
	 @Resource
	 private CartItemRepository cartItemRepository;
	 @Resource
	 private ProductMediaRepository productMediaRepository;
	 @Resource
	 private CartEntityRepository cartEntityRepository;

	protected void init(){
		cartEntityDao = SpringHelper.getBean(CartEntityDao.class);
		setDao(cartEntityDao);
	}

	public void saveAndItem(CartEntityPo cartEntityPo, Integer qty, String productId,String skuId) {
		if(qty == null){
			qty = 1;
		}
		cartEntityRepository.newInstance(cartEntityPo).create();
		CartItemPo cartItemPo = new CartItemPo();
		cartItemPo.setCartId(cartEntityPo.getId());
		cartItemPo.setProdSkuId(skuId);
		cartItemPo.setIsSaved('N');
		cartItemPo.setIsMain('Y');
		cartItemPo.setQty(qty);
		cartItemPo.setSalesType("stock");
		cartItemPo.setCreateTime(new Date());
		ProductMediaPo productMediaPo = productMediaRepository.getMainMediaPoByProdId(productId);
		if(productMediaPo !=null){
			cartItemPo.setMainCloudPath(productMediaPo.getCloudPath());
		}
		CartItem cartItem = cartItemRepository.newInstance(cartItemPo);
		cartItem.create();
	}	 
	 
}
